package com.huangsm.juc.volatile1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * CopyOnWriteArrayList/CopyOnWriteArraySet："写入并复制"
 */
public class TestCopyOnWriteArrayList {
    public static void main(String[] args) {
        HelloThread ht=new HelloThread();
        for (int i = 0; i < 10; i++) {
            new Thread(ht).start();
        }
    }
}
class HelloThread implements Runnable{
    //java.util.ConcurrentModificationException
    //private static List<String> list= Collections.synchronizedList(new ArrayList<>());
    private static List<String> list=new  CopyOnWriteArrayList<>();
    static {
        list.add("aa");
        list.add("bb");
        list.add("cc");
    }

    @Override
    public void run() {
        Iterator<String> it = list.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
            list.add("aa");
        }
    }
}
